/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2011
 */

package com.nail.zhaogr.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.util.FreemarkerUtils;
import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.web.scope.Flash;

import com.nail.zhaogr.model.PublishInfo;
import com.nail.zhaogr.service.PublishInfoManager;
import com.nail.zhaogr.vo.query.PublishInfoQuery;

/**
 * 
 * @author Nail
 *
 */
public class HtmlAction extends BaseAction{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = "credit asc,updateTime desc"; 
	
	//forward paths
	protected static final String GEN_HTML = "/pages/ronail/genHtml.jsp";
	
	private PublishInfoManager publishInfoManager;
	
	private PublishInfo publishInfo;
	
	java.lang.Long id = null;
	private String[] items;

	public void prepare() throws Exception {
		init();
		if (isNullOrEmptyString(id)) {
			publishInfo = new PublishInfo();
		} else {
			publishInfo = (PublishInfo)publishInfoManager.getById(id);
		}
	}
	
	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setPublishInfoManager(PublishInfoManager manager) {
		this.publishInfoManager = manager;
	}	
	
	public Object getModel() {
		return publishInfo;
	}
	
	public void setPublishInfoId(java.lang.Long val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}
	
	public String index(){
		return GEN_HTML;
	}
	
	public String genIndex(){
		genIndexHtml();
		Flash.current().success("生成成功");
		return GEN_HTML;
	}
	
	public String genStar(){
		genStarHtml();
		Flash.current().success("生成成功");
		return GEN_HTML;
	}
	
	public String genHot(){
		genHotHtml();
		Flash.current().success("生成成功");
		return GEN_HTML;
	}
	
	protected void genIndexHtml(){
		PublishInfoQuery query = newQuery(PublishInfoQuery.class,DEFAULT_SORT_COLUMNS);
		query.setParentCityId(1372L);
		Page page = publishInfoManager.findPage(query);
		limitLength(page);
		List<PublishInfo> workerList = page.getResult();
		
		Map propMap = new HashMap();
		propMap.put("workerList", workerList);
		//propMap.put("linkTitle", "友情链接");
		String classPath = FreemarkerUtils.class.getResource("/").getPath();
		String sGeneFilePath = classPath.substring(0, classPath.indexOf("WEB-INF"));
		FreemarkerUtils.geneHtmlFile("index.ftl", propMap, sGeneFilePath, "index.html");
	}
	
	
	protected void genStarHtml(){
		try {
			PublishInfoQuery query = newQuery(PublishInfoQuery.class,"quality desc,updateTime asc");
			query.setParentCityId(1372L);
			Page page = publishInfoManager.findPage(query);
			List<PublishInfo> qualityStarList = page.getResult();
			
			PublishInfoQuery attitudeQuery = newQuery(PublishInfoQuery.class,"attitude desc,updateTime asc");
			query.setParentCityId(1372L);
			Page attitudePage = publishInfoManager.findPage(attitudeQuery);
			List<PublishInfo> attitudeStarList = attitudePage.getResult();
			
			Map propMap = new HashMap();
			propMap.put("qualityStarList", qualityStarList);
			propMap.put("attitudeStarList", attitudeStarList);
			
			//propMap.put("linkTitle", "友情链接");
			String classPath = FreemarkerUtils.class.getResource("/").getPath();
			String sGeneFilePath = classPath.substring(0, classPath.indexOf("WEB-INF"))+"commons";
			//FreemarkerUtils.geneHtmlFile("qualityStar.ftl", propMap, sGeneFilePath, "qualityStar.html");
			FreemarkerUtils.geneHtmlFile("star.ftl", propMap, sGeneFilePath, "star.html");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	protected void genHotHtml(){
		PublishInfoQuery query = newQuery(PublishInfoQuery.class,"commendCount desc,favoriteCount desc");
		query.setParentCityId(1372L);
		Page page = publishInfoManager.findPage(query);
		List<PublishInfo> workerList = page.getResult();
		
		Map propMap = new HashMap();
		propMap.put("workerList", workerList);
		//propMap.put("linkTitle", "友情链接");
		String classPath = FreemarkerUtils.class.getResource("/").getPath();
		String sGeneFilePath = classPath.substring(0, classPath.indexOf("WEB-INF"))+"commons";
		FreemarkerUtils.geneHtmlFile("hot.ftl", propMap, sGeneFilePath, "hot.html");
	}
	

}
